package com.zj.joke;

import java.util.HashMap;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.renren.Renren;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.tencent.weibo.TencentWeibo;
import cn.waps.AppConnect;

import com.zj.joke.R;
import com.zj.joke.util.ActivityUtils;
import com.zj.joke.view.QuitPopAd;
import com.zj.joke.view.SwitchButton;
import com.zj.joke.view.SwitchButton.OnSwitchListener;

public class SettingActivity extends ZJActivity implements OnClickListener,
		OnSwitchListener {
	private TextView backTv;
	private View aboutView;
	private SwitchButton mSinaAuthSB;
	private SwitchButton mTencentAuthSB;
	private SwitchButton mQZoneAuthSB;
	private SwitchButton mRenrenAuthSB;
	private SwitchButton mSettingMode;
	private TextView mSinaAuthTitle;
	private TextView mTencentAuthTitle;
	private TextView mQZoneAuthTitle;
	private TextView mRenrenAuthTitle;
	protected MyHandler handler;
	private Platform sinaPlatform;
	private Platform tencentPlatform;
	private Platform qzonePlatform;
	private Platform renrenPlatform;
	private AuthListener authorizeListener;
	private cancelAnthListener cancelAnthListener;
	private final int MSG_AUTH_COMPLETE = 1000;
	private final int MSG_AUTH_CANCEL = 1001;
	private final int MSG_AUTH_ERROR = 1002;
	private final int MSG_CANCEL_AUTH_COMPLETE = 1003;
	private final int MSG_CANCEL_AUTH_CANCEL = 1004;
	private final int MSG_CANCEL_AUTH_ERROR = 1005;
	private Resources mResources;
	private View mExitLayout;
	private View mMoneyLayout;
	private View mFeedBackLayout;
	private View mHelpLayout;
	private View mUpdateLayout;
	private SharedPreferences mPreferences;
	private boolean mMode;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		findViews();
		handler = new MyHandler();
		initPlatform();
	}

	private void findViews() {
		mResources = getResources();
		backTv = (TextView) findViewById(R.id.settingback);
		backTv.setOnClickListener(this);
		aboutView = findViewById(R.id.settingAboutLayout);
		aboutView.setOnClickListener(this);

		mSinaAuthSB = (SwitchButton) findViewById(R.id.settingSinaAuthSB);
		mSinaAuthSB.setOnSwitchListener(this);

		mTencentAuthSB = (SwitchButton) findViewById(R.id.settingTencentAuthSB);
		mTencentAuthSB.setOnSwitchListener(this);

		mQZoneAuthSB = (SwitchButton) findViewById(R.id.settingQZoneAuthSB);
		mQZoneAuthSB.setOnSwitchListener(this);

		mRenrenAuthSB = (SwitchButton) findViewById(R.id.settingRenrenAuthSB);
		mRenrenAuthSB.setOnSwitchListener(this);

		mSinaAuthTitle = (TextView) findViewById(R.id.settingSinaAuthTitle);
		mTencentAuthTitle = (TextView) findViewById(R.id.settingTencentAuthTitle);
		mQZoneAuthTitle = (TextView) findViewById(R.id.settingQZoneAuthTitle);
		mRenrenAuthTitle = (TextView) findViewById(R.id.settingRenrenAuthTitle);

		mExitLayout = findViewById(R.id.settingExitLayout);
		mExitLayout.setOnClickListener(this);
		mMoneyLayout = findViewById(R.id.settingMoneyLayout);
		mMoneyLayout.setOnClickListener(this);
		mFeedBackLayout = findViewById(R.id.settingFeedBackLayout);
		mFeedBackLayout.setOnClickListener(this);
		mHelpLayout = findViewById(R.id.settingHelpLayout);
		mHelpLayout.setOnClickListener(this);
		mUpdateLayout = findViewById(R.id.settingUpdateLayout);
		mUpdateLayout.setOnClickListener(this);
		// mSettingMode = (SwitchButton) findViewById(R.id.settingMode);
		// mSettingMode.setOnSwitchListener(this);
		// mPreferences = getSharedPreferences(Consts.SP_NAME,
		// Context.MODE_PRIVATE);
		// mMode = mPreferences.getBoolean(Consts.SP_KEY_MODE, false);
		// mSettingMode.setSwitch(mMode);
		// JsoupDemoApp.getInstance().setInitDataMode(mMode);
	}

	private class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			Platform plat = (Platform) msg.obj;
			SwitchButton switchButton = null;
			TextView titleTv = null;
			String title = null;
			if (!TextUtils.isEmpty(plat.getName())) {
				if (SinaWeibo.NAME.equals(plat.getName())) {
					switchButton = mSinaAuthSB;
					titleTv = mSinaAuthTitle;
					title = mResources.getString(R.string.sinaweibo);
				} else if (TencentWeibo.NAME.equals(plat.getName())) {
					switchButton = mTencentAuthSB;
					titleTv = mTencentAuthTitle;
					title = mResources.getString(R.string.tencentweibo);
				} else if (Renren.NAME.equals(plat.getName())) {
					switchButton = mRenrenAuthSB;
					titleTv = mRenrenAuthTitle;
					title = mResources.getString(R.string.renren);
				} else if (QZone.NAME.equals(plat.getName())) {
					switchButton = mQZoneAuthSB;
					titleTv = mQZoneAuthTitle;
					title = mResources.getString(R.string.qzone);
				}
			}
			switch (msg.arg1) {
			case MSG_AUTH_COMPLETE:
				switchButton.setSwitch(true);
				titleTv.setText(title + "(" + plat.getDb().get("nickname")
						+ ")");
				Toast.makeText(mContext, "绑定成功", Toast.LENGTH_SHORT).show();
				break;
			case MSG_AUTH_ERROR:
				// switchButton.setSwitch(false);
				titleTv.setText(title);
				Toast.makeText(mContext, "绑定失败", Toast.LENGTH_SHORT).show();
				break;
			case MSG_AUTH_CANCEL:
				// switchButton.setSwitch(false);
				// titleTv.setText(mResources.getString(R.string.sinaweibo));
				Toast.makeText(mContext, "取消操作", Toast.LENGTH_SHORT).show();
				break;
			case MSG_CANCEL_AUTH_COMPLETE:
				switchButton.setSwitch(false);
				titleTv.setText(title);
				Toast.makeText(mContext, "解绑成功", Toast.LENGTH_SHORT).show();
				break;
			case MSG_CANCEL_AUTH_ERROR:
				// titleTv.setText(mResources.getString(R.string.sinaweibo) +
				// "("
				// + plat.getDb().get("nickname") + ")");
				Toast.makeText(mContext, "解绑失败", Toast.LENGTH_SHORT).show();
				break;
			case MSG_CANCEL_AUTH_CANCEL:
				// titleTv.setText(mResources.getString(R.string.sinaweibo) +
				// "("
				// + plat.getDb().get("nickname") + ")");
				Toast.makeText(mContext, "取消操作", Toast.LENGTH_SHORT).show();
				break;
			}
		}
	}

	@Override
	public void onBackPressed() {
		finish();
		overridePendingTransition(R.anim.push_left_in, R.anim.push_right_out);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.settingback:
			finish();
			overridePendingTransition(R.anim.push_left_in,
					R.anim.push_right_out);
			break;
		case R.id.settingAboutLayout:
			Intent intent2 = new Intent();
			intent2.setClass(SettingActivity.this, AboutActivity.class);
			startActivity(intent2);
			overridePendingTransition(R.anim.activity_zoom_in,
					R.anim.activity_zoom_out);

			// final Dialog dialog = new Dialog(SettingActivity.this,
			// R.style.common_trans_dialog);
			// View dialogView = LayoutInflater.from(SettingActivity.this)
			// .inflate(R.layout.setting_about_dialog, null);
			// dialog.setContentView(dialogView);
			// dialog.show();

			break;
		case R.id.settingExitLayout:
			String value = AppConnect.getInstance(this).getConfig("say_hello",
					"no");
			if ("no".equals(value)) {
				exitApp();
			} else {
				QuitPopAd.getInstance().show2(this);
			}
			// exitApp();
			break;
		case R.id.settingMoneyLayout:
			AppConnect.getInstance(mContext).showOffers(this);
			break;
		case R.id.settingFeedBackLayout:
			AppConnect.getInstance(this).showFeedback(this);
			break;
		case R.id.settingHelpLayout:
			Intent intent = new Intent(SettingActivity.this, InitActivity.class);
			intent.putExtra(Consts.GUIDE_FLAG, true);
			startActivity(intent);
			overridePendingTransition(R.anim.activity_zoom_in,
					R.anim.activity_zoom_out);
			break;
		case R.id.settingUpdateLayout:
			AppConnect.getInstance(mContext).checkUpdate(mContext);
			break;
		}
	}

	private void exitApp() {
		Dialog alertDialog = new AlertDialog.Builder(this).setTitle("提示")
				.setMessage("确定要退出吗？")
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				})
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						AppConnect.getInstance(mContext).close();
						ShareSDK.stopSDK(mContext);
						ActivityUtils.getInstance().CloseAll();
					}
				}).create();
		alertDialog.show();
	}

	private void initPlatform() {
		sinaPlatform = ShareSDK.getPlatform(mContext, SinaWeibo.NAME);
		tencentPlatform = ShareSDK.getPlatform(mContext, TencentWeibo.NAME);
		qzonePlatform = ShareSDK.getPlatform(mContext, QZone.NAME);
		renrenPlatform = ShareSDK.getPlatform(mContext, Renren.NAME);
		authorizeListener = new AuthListener();
		cancelAnthListener = new cancelAnthListener();
		if (sinaPlatform.isValid()) {
			mSinaAuthTitle.setText(mResources.getString(R.string.sinaweibo)
					+ "(" + sinaPlatform.getDb().get("nickname") + ")");
			mSinaAuthSB.setSwitch(true);
		} else {
			mSinaAuthTitle.setText(mResources.getString(R.string.sinaweibo));
			mSinaAuthSB.setSwitch(false);
		}
		if (tencentPlatform.isValid()) {
			mTencentAuthTitle.setText(mResources
					.getString(R.string.tencentweibo)
					+ "("
					+ tencentPlatform.getDb().get("nickname") + ")");
			mTencentAuthSB.setSwitch(true);
		} else {
			mTencentAuthTitle.setText(mResources
					.getString(R.string.tencentweibo));
			mTencentAuthSB.setSwitch(false);
		}
		if (renrenPlatform.isValid()) {
			mRenrenAuthTitle.setText(mResources.getString(R.string.renren)
					+ "(" + renrenPlatform.getDb().get("nickname") + ")");
			mRenrenAuthSB.setSwitch(true);
		} else {
			mRenrenAuthTitle.setText(mResources.getString(R.string.renren));
			mRenrenAuthSB.setSwitch(false);
		}
		if (qzonePlatform.isValid()) {
			mQZoneAuthTitle.setText(mResources.getString(R.string.qzone) + "("
					+ qzonePlatform.getDb().get("nickname") + ")");
			mQZoneAuthSB.setSwitch(true);
		} else {
			mQZoneAuthTitle.setText(mResources.getString(R.string.qzone));
			mQZoneAuthSB.setSwitch(false);
		}
	}

	@Override
	public boolean onSwitch(SwitchButton v, boolean isRight) {
		switch (v.getId()) {
		case R.id.settingSinaAuthSB:
			authPlatform(v, sinaPlatform);
			break;
		case R.id.settingTencentAuthSB:
			authPlatform(v, tencentPlatform);
			break;

		case R.id.settingRenrenAuthSB:
			authPlatform(v, renrenPlatform);
			break;
		case R.id.settingQZoneAuthSB:
			authPlatform(v, qzonePlatform);
			break;
		case R.id.settingMode:
			mSettingMode.setSwitch(isRight);
			Editor editor = mPreferences.edit();
			editor.putBoolean(Consts.SP_KEY_MODE, isRight);
			editor.commit();
			JokeApp.getInstance().setInitDataMode(isRight);
			break;
		}
		return true;
	}

	public void authPlatform(SwitchButton switchButton, Platform plat) {
		if (plat.isValid()) {
			Message msg = new Message();
			msg.arg1 = MSG_CANCEL_AUTH_COMPLETE;
			msg.obj = plat;
			handler.sendMessage(msg);
			plat.removeAccount();
		} else {
			plat.setPlatformActionListener(authorizeListener);
			plat.authorize();
		}
	}

	private class AuthListener implements PlatformActionListener {
		@Override
		public void onComplete(Platform plat, int action,
				HashMap<String, Object> res) {
			Message msg = new Message();
			msg.arg1 = MSG_AUTH_COMPLETE;
			msg.arg2 = action;
			msg.obj = plat;
			handler.sendMessage(msg);
		}

		@Override
		public void onError(Platform plat, int action, Throwable t) {
			t.printStackTrace();
			Message msg = new Message();
			msg.arg1 = MSG_AUTH_ERROR;
			msg.arg2 = action;
			msg.obj = plat;
			handler.sendMessage(msg);
		}

		@Override
		public void onCancel(Platform plat, int action) {
			Message msg = new Message();
			msg.arg1 = MSG_AUTH_CANCEL;
			msg.arg2 = action;
			msg.obj = plat;
			handler.sendMessage(msg);
		}

	}

	private class cancelAnthListener implements PlatformActionListener {
		@Override
		public void onComplete(Platform plat, int action,
				HashMap<String, Object> res) {

			Message msg = new Message();
			msg.arg1 = MSG_CANCEL_AUTH_COMPLETE;
			msg.arg2 = action;
			msg.obj = plat;
			handler.sendMessage(msg);
		}

		@Override
		public void onError(Platform plat, int action, Throwable t) {
			t.printStackTrace();
			Message msg = new Message();
			msg.arg1 = MSG_CANCEL_AUTH_ERROR;
			msg.arg2 = action;
			msg.obj = plat;
			handler.sendMessage(msg);
		}

		@Override
		public void onCancel(Platform plat, int action) {
			Message msg = new Message();
			msg.arg1 = MSG_CANCEL_AUTH_CANCEL;
			msg.arg2 = action;
			msg.obj = plat;
			handler.sendMessage(msg);
		}
	}
}
